package com.jiazhong.servlet.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.commons.bean.Emp;
import com.jiazhong.servlet.mapper.EmpMapper;
import com.jiazhong.servlet.service.EmpService;
import com.jiazhong.servlet.test.App;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class EmpServiceImpl implements EmpService {

    private EmpMapper mapper;
    private SqlSessionFactory sqlSessionFactory;

    public EmpServiceImpl() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Override
    public JsonResult findAll() {
        SqlSession session = sqlSessionFactory.openSession(true);
        mapper = session.getMapper(EmpMapper.class);
        // 1. 调用mapper的findAll方法
        List<Emp> list = mapper.findAll();
        // 2. 将list--->String
        String str = JSONArray.toJSONString(list);
        session.close();
        return new JsonResult(str);
    }

    @Override
    public JsonResult findByPage(int page, int size) {
        SqlSession session = sqlSessionFactory.openSession(true);
        mapper = session.getMapper(EmpMapper.class);
        PageHelper.startPage(page, size);
        List<Emp> list = mapper.findAll();
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        String str = JSONArray.toJSONString(pageInfo);
        return new JsonResult(str);
    }

    @Override
    public JsonResult save(Emp emp) {
        SqlSession session = sqlSessionFactory.openSession(true);
        mapper = session.getMapper(EmpMapper.class);
        int save = 0;
        try {
            save = mapper.save(emp);
        } catch (Exception e) {
            return new JsonResult(500, e.getMessage());
        }
        if (save == 0) {
            return new JsonResult(500, "添加失败");
        }
        session.clearCache();
        return new JsonResult("添加成功");
    }

    @Override
    public JsonResult update(Emp emp) {
        SqlSession session = sqlSessionFactory.openSession(true);
        mapper = session.getMapper(EmpMapper.class);
        int row = mapper.update(emp);
        if (row == 0) {
            return new JsonResult(500, "修改失败");
        }
        session.clearCache();
        return new JsonResult("修改成功");
    }

    @Override
    public JsonResult findById(int id) {
        SqlSession session = sqlSessionFactory.openSession(true);
        mapper = session.getMapper(EmpMapper.class);
        Emp emp = mapper.findById(id);
        String str = JSONArray.toJSONString(emp);
        return new JsonResult(str);
    }

    @Override
    public JsonResult delete(int id) {
        SqlSession session = sqlSessionFactory.openSession(true);
        mapper = session.getMapper(EmpMapper.class);
        int row = mapper.delete(id);
        if (row == 0) {
            return new JsonResult(500, "删除失败");
        }
        session.clearCache();
        return new JsonResult("删除成功");
    }

    public JsonResult findByEname(String ename, int page) {
        SqlSession session = sqlSessionFactory.openSession(true);
        mapper = session.getMapper(EmpMapper.class);
        PageHelper.startPage(page, 10);
        List<Emp> list = mapper.findByEname(ename == null ? "%%" : "%" + ename + "%");
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        String str = JSONArray.toJSONString(pageInfo);
        return new JsonResult(str);
    }
}
